{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# default_exp models.MINIROCKET"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MINIROCKET\n",
    "\n",
    "> A Very Fast (Almost) Deterministic Transform for Time Series Classification."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "from tsai.imports import *\n",
    "from tsai.utils import *\n",
    "from tsai.data.external import *\n",
    "from tsai.models.layers import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "from sktime.transformations.panel.rocket import MiniRocketMultivariate\n",
    "from sklearn.linear_model import RidgeCV, RidgeClassifierCV\n",
    "from sklearn.ensemble import VotingClassifier, VotingRegressor\n",
    "import warnings\n",
    "warnings.simplefilter(action='ignore', category=FutureWarning)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "class MiniRocketClassifier(sklearn.pipeline.Pipeline):\n",
    "    \"\"\"Time series classification using MINIROCKET features and a linear classifier\"\"\"\n",
    "    def __init__(self, num_features=10_000, max_dilations_per_kernel=32, random_state=None,\n",
    "                 alphas=np.logspace(-3, 3, 7), normalize_features=True, memory=None, verbose=False, scoring=None, class_weight=None, **kwargs):\n",
    "        \"\"\"\n",
    "        MiniRocketClassifier is recommended for up to 10k time series. \n",
    "        For a larger dataset, you can use MINIROCKET (in Pytorch).\n",
    "        scoring = None --> defaults to accuracy.\n",
    "        \"\"\"\n",
    "        self.steps = [('minirocketmultivariate', MiniRocketMultivariate(num_features=num_features, \n",
    "                                                                        max_dilations_per_kernel=max_dilations_per_kernel,\n",
    "                                                                        random_state=random_state)),\n",
    "                      ('ridgeclassifiercv', RidgeClassifierCV(alphas=alphas, \n",
    "                                                              normalize=normalize_features, \n",
    "                                                              scoring=scoring, \n",
    "                                                              class_weight=class_weight, \n",
    "                                                              **kwargs))]\n",
    "        store_attr()\n",
    "        self._validate_steps()\n",
    "\n",
    "    def __repr__(self):\n",
    "        return f'Pipeline(steps={self.steps.copy()})'\n",
    "\n",
    "    def save(self, fname=None, path='./models'):\n",
    "        fname = ifnone(fname, 'MiniRocketClassifier')\n",
    "        path = Path(path)\n",
    "        filename = path/fname\n",
    "        filename.parent.mkdir(parents=True, exist_ok=True)\n",
    "        with open(f'{filename}.pkl', 'wb') as output:\n",
    "            pickle.dump(self, output, pickle.HIGHEST_PROTOCOL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "def load_minirocket(fname, path='./models'):\n",
    "    path = Path(path)\n",
    "    filename = path/fname\n",
    "    with open(f'{filename}.pkl', 'rb') as input:\n",
    "        output = pickle.load(input)\n",
    "    return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "class MiniRocketRegressor(sklearn.pipeline.Pipeline):\n",
    "    \"\"\"Time series regression using MINIROCKET features and a linear regressor\"\"\"\n",
    "    def __init__(self, num_features=10000, max_dilations_per_kernel=32, random_state=None,\n",
    "                 alphas=np.logspace(-3, 3, 7), *, normalize_features=True, memory=None, verbose=False, scoring=None, **kwargs):\n",
    "        \"\"\"\n",
    "        MiniRocketRegressor is recommended for up to 10k time series. \n",
    "        For a larger dataset, you can use MINIROCKET (in Pytorch).\n",
    "        scoring = None --> defaults to r2.\n",
    "        \"\"\"\n",
    "        self.steps = [('minirocketmultivariate', MiniRocketMultivariate(num_features=num_features,\n",
    "                                                                        max_dilations_per_kernel=max_dilations_per_kernel,\n",
    "                                                                        random_state=random_state)),\n",
    "                      ('ridgecv', RidgeCV(alphas=alphas, normalize=normalize_features, scoring=scoring, **kwargs))]\n",
    "        store_attr()\n",
    "        self._validate_steps()\n",
    "\n",
    "    def __repr__(self):\n",
    "        return f'Pipeline(steps={self.steps.copy()})'\n",
    "\n",
    "    def save(self, fname=None, path='./models'):\n",
    "        fname = ifnone(fname, 'MiniRocketRegressor')\n",
    "        path = Path(path)\n",
    "        filename = path/fname\n",
    "        filename.parent.mkdir(parents=True, exist_ok=True)\n",
    "        with open(f'{filename}.pkl', 'wb') as output:\n",
    "            pickle.dump(self, output, pickle.HIGHEST_PROTOCOL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "def load_minirocket(fname, path='./models'):\n",
    "    path = Path(path)\n",
    "    filename = path/fname\n",
    "    with open(f'{filename}.pkl', 'rb') as input:\n",
    "        output = pickle.load(input)\n",
    "    return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "class MiniRocketVotingClassifier(VotingClassifier):\n",
    "    \"\"\"Time series classification ensemble using MINIROCKET features, a linear classifier and majority voting\"\"\"\n",
    "    def __init__(self, n_estimators=5, weights=None, n_jobs=-1, num_features=10_000, max_dilations_per_kernel=32, random_state=None, \n",
    "                 alphas=np.logspace(-3, 3, 7), normalize_features=True, memory=None, verbose=False, scoring=None, class_weight=None, **kwargs):\n",
    "        store_attr()\n",
    "        estimators = [(f'est_{i}', MiniRocketClassifier(num_features=num_features, max_dilations_per_kernel=max_dilations_per_kernel, \n",
    "                                                       random_state=random_state, alphas=alphas, normalize_features=normalize_features, memory=memory, \n",
    "                                                       verbose=verbose, scoring=scoring, class_weight=class_weight, **kwargs)) \n",
    "                    for i in range(n_estimators)]\n",
    "        super().__init__(estimators, voting='hard', weights=weights, n_jobs=n_jobs, verbose=verbose)\n",
    "\n",
    "    def __repr__(self):   \n",
    "        return f'MiniRocketVotingClassifier(n_estimators={self.n_estimators}, \\nsteps={self.estimators[0][1].steps})'\n",
    "\n",
    "    def save(self, fname=None, path='./models'):\n",
    "        fname = ifnone(fname, 'MiniRocketVotingClassifier')\n",
    "        path = Path(path)\n",
    "        filename = path/fname\n",
    "        filename.parent.mkdir(parents=True, exist_ok=True)\n",
    "        with open(f'{filename}.pkl', 'wb') as output:\n",
    "            pickle.dump(self, output, pickle.HIGHEST_PROTOCOL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "def get_minirocket_preds(X, fname, path='./models', model=None):\n",
    "    if X.ndim == 1: X = X[np.newaxis][np.newaxis]\n",
    "    elif X.ndim == 2: X = X[np.newaxis]\n",
    "    if model is None: \n",
    "        model = load_minirocket(fname=fname, path=path)\n",
    "    return model.predict(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "class MiniRocketVotingRegressor(VotingRegressor):\n",
    "    \"\"\"Time series regression ensemble using MINIROCKET features, a linear regressor and a voting regressor\"\"\"\n",
    "    def __init__(self, n_estimators=5, weights=None, n_jobs=-1, num_features=10_000, max_dilations_per_kernel=32, random_state=None,\n",
    "                 alphas=np.logspace(-3, 3, 7), normalize_features=True, memory=None, verbose=False, scoring=None, **kwargs):\n",
    "        store_attr()\n",
    "        estimators = [(f'est_{i}', MiniRocketRegressor(num_features=num_features, max_dilations_per_kernel=max_dilations_per_kernel,\n",
    "                                                      random_state=random_state, alphas=alphas, normalize_features=normalize_features, memory=memory,\n",
    "                                                      verbose=verbose, scoring=scoring, **kwargs))\n",
    "                      for i in range(n_estimators)]\n",
    "        super().__init__(estimators, weights=weights, n_jobs=n_jobs, verbose=verbose)\n",
    "\n",
    "    def __repr__(self):\n",
    "        return f'MiniRocketVotingRegressor(n_estimators={self.n_estimators}, \\nsteps={self.estimators[0][1].steps})'\n",
    "\n",
    "    def save(self, fname=None, path='./models'):\n",
    "        fname = ifnone(fname, 'MiniRocketVotingRegressor')\n",
    "        path = Path(path)\n",
    "        filename = path/fname\n",
    "        filename.parent.mkdir(parents=True, exist_ok=True)\n",
    "        with open(f'{filename}.pkl', 'wb') as output:\n",
    "            pickle.dump(self, output, pickle.HIGHEST_PROTOCOL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Univariate classification with sklearn-type API\n",
    "dsid = 'OliveOil'\n",
    "fname = 'MiniRocketClassifier'\n",
    "X_train, y_train, X_test, y_test = get_UCR_data(dsid)\n",
    "cls = MiniRocketClassifier()\n",
    "cls.fit(X_train, y_train)\n",
    "cls.save(fname)\n",
    "pred = cls.score(X_test, y_test)\n",
    "del cls\n",
    "cls = load_minirocket(fname)\n",
    "test_eq(cls.score(X_test, y_test), pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9277777777777778"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Multivariate classification with sklearn-type API\n",
    "dsid = 'NATOPS'\n",
    "X_train, y_train, X_test, y_test = get_UCR_data(dsid)\n",
    "cls = MiniRocketClassifier()\n",
    "cls.fit(X_train, y_train)\n",
    "cls.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9222222222222223"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Multivariate classification with sklearn-type API\n",
    "dsid = 'NATOPS'\n",
    "X_train, y_train, X_test, y_test = get_UCR_data(dsid)\n",
    "cls = MiniRocketVotingClassifier(5)\n",
    "cls.fit(X_train, y_train)\n",
    "cls.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.041662470066026296\n"
     ]
    }
   ],
   "source": [
    "# Univariate regression with sklearn-type API\n",
    "from sklearn.metrics import mean_squared_error\n",
    "dsid = 'Covid3Month'\n",
    "fname = 'MiniRocketRegressor'\n",
    "X_train, y_train, X_test, y_test = get_Monash_regression_data(dsid)\n",
    "if X_train is not None:\n",
    "    rmse_scorer = make_scorer(mean_squared_error, greater_is_better=False)\n",
    "    reg = MiniRocketRegressor(scoring=rmse_scorer)\n",
    "    reg.fit(X_train, y_train)\n",
    "    reg.save(fname)\n",
    "    del reg\n",
    "    reg = load_minirocket(fname)\n",
    "    y_pred = reg.predict(X_test)\n",
    "    print(mean_squared_error(y_test, y_pred, squared=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.1373083702955604\n"
     ]
    }
   ],
   "source": [
    "# Multivariate regression with sklearn-type API\n",
    "from sklearn.metrics import mean_squared_error\n",
    "dsid = 'AppliancesEnergy'\n",
    "X_train, y_train, X_test, y_test = get_Monash_regression_data(dsid)\n",
    "if X_train is not None:\n",
    "    rmse_scorer = make_scorer(mean_squared_error, greater_is_better=False)\n",
    "    reg = MiniRocketRegressor(scoring=rmse_scorer)\n",
    "    reg.fit(X_train, y_train)\n",
    "    reg.save(fname)\n",
    "    del reg\n",
    "    reg = load_minirocket(fname)\n",
    "    y_pred = reg.predict(X_test)\n",
    "    print(mean_squared_error(y_test, y_pred, squared=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.2439973175077133\n"
     ]
    }
   ],
   "source": [
    "# Multivariate regression ensemble with sklearn-type API\n",
    "if X_train is not None:\n",
    "    reg = MiniRocketVotingRegressor(5, scoring=rmse_scorer)\n",
    "    reg.fit(X_train, y_train)\n",
    "    y_pred = reg.predict(X_test)\n",
    "    print(mean_squared_error(y_test, y_pred, squared=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#hide\n",
    "from tsai.imports import create_scripts\n",
    "from tsai.export import get_nb_name\n",
    "nb_name = get_nb_name()\n",
    "create_scripts(nb_name);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
